package cn.stylefeng.guns.modular.school.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.comm.util.DictTypeConstant;
import cn.stylefeng.guns.modular.school.entity.ClassInfo;
import cn.stylefeng.guns.modular.school.entity.ClassTeacherLession;
import cn.stylefeng.guns.modular.school.model.params.ClassTeacherLessionParam;
import cn.stylefeng.guns.modular.school.model.result.TeacherInfoResult;
import cn.stylefeng.guns.modular.school.service.ClassInfoService;
import cn.stylefeng.guns.modular.school.service.ClassTeacherLessionService;
import cn.stylefeng.guns.modular.school.service.TeacherInfoService;
import cn.stylefeng.guns.sys.modular.system.entity.Dict;
import cn.stylefeng.guns.sys.modular.system.service.DictService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 班级教师课程关系控制器
 *
 * @author
 * @Date 2019-12-01 16:16:42
 */
@Controller
@RequestMapping("/classTeacherLession")
public class ClassTeacherLessionController extends BaseController {

    private String PREFIX = "/school/classTeacherLession";

    @Autowired
    private ClassTeacherLessionService classTeacherLessionService;
    @Autowired
    private ClassInfoService classInfoService;
    @Autowired
    private TeacherInfoService teacherInfoService;
    @Autowired
    private DictService dictService;
    /**
     * 跳转到主页面
     *
     * @author
     * @Date 2019-12-01
     */
    @RequestMapping("")
    public String index(int classId) {
        setAttr("classId", classId);
        return PREFIX + "/classTeacherLession.html";
    }

    /**
     * 新增页面
     *
     * @author
     * @Date 2019-12-01
     */
    @RequestMapping("/add")
    public String add(int classId) {
        setAttr("classId", classId);
        ClassInfo classInfo = classInfoService.getById(classId);
        setAttr("classInfo", classInfo);
        init();
        return PREFIX + "/classTeacherLession_add.html";
    }

    private void init() {
        // 教师
        // TODO 先取得所有教师，以后有需求再改成根据学校取得本校的教师
        List<TeacherInfoResult> teachers = teacherInfoService.findListBySpec(null);
        teachers.forEach(teacher -> {
            teacher.setName(teacher.getCode() + "  " + teacher.getSexName() + "  " + teacher.getName());
        });
        setAttr("teachers", teachers);
        // 课程
        List<Dict> LESSION = dictService.listDictsByCode(DictTypeConstant.LESSION);
        setAttr("LESSION", LESSION);
    }

    /**
     * 编辑页面
     *
     * @author
     * @Date 2019-12-01
     */
    @RequestMapping("/edit")
    public String edit() {
        init();
        return PREFIX + "/classTeacherLession_edit.html";
    }

    /**
     * 新增接口
     *
     * @author
     * @Date 2019-12-01
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ClassTeacherLessionParam classTeacherLessionParam) {
        this.classTeacherLessionService.add(classTeacherLessionParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author
     * @Date 2019-12-01
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ClassTeacherLessionParam classTeacherLessionParam) {
        this.classTeacherLessionService.update(classTeacherLessionParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author
     * @Date 2019-12-01
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ClassTeacherLessionParam classTeacherLessionParam) {
        this.classTeacherLessionService.delete(classTeacherLessionParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author
     * @Date 2019-12-01
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ClassTeacherLessionParam classTeacherLessionParam) {
        ClassTeacherLession detail = this.classTeacherLessionService.getById(classTeacherLessionParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author
     * @Date 2019-12-01
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ClassTeacherLessionParam classTeacherLessionParam) {
        return this.classTeacherLessionService.findPageBySpec(classTeacherLessionParam);
    }

}


